package com.example.exam.controller;

import com.example.exam.entity.*;
import com.example.exam.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * (correct)表控制层
 *
 * @author makejava
 * @since 2021-09-04 15:02:44
 */
@RestController
@RequestMapping("/correct")
public class CorrectController {
    @Autowired
    private AssistService assistService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private StuansService stuansService;
    @Autowired
    private PaperService paperService;
    @Autowired
    private ChoiceService choiceService;
    @Autowired
    private FillbacksService fillbacksService;
    @Autowired
    private TorfService torfService;
    @Autowired
    private SubjectiveService subjectiveService;
    @Autowired
    private GradeService gradeService;

    /**
     * @return 所有助教直属的试题
     */
    @RequestMapping("/allquestion")
    public List<Question> AllQuestion(HttpSession session) {
        int assId = (int) session.getAttribute("userId");
        Assist assist = assistService.queryById(assId);
        int collegeId = assist.getcollegeId();
        List<Integer> majorIds = new LinkedList<>();
        List<Major> majors = majorService.queryAll();
        for (Major major : majors) {
            if (major.getCollegerid() == collegeId) {
                majorIds.add(major.getMajorid());
            }
        }
        Set<Question> res = new HashSet<>();
        List<Question> questions = questionService.queryAll2();
        for (int i = 0; i < majorIds.size(); i++) {
            for (int j = 0; j < questions.size(); j++) {
                if (questions.get(j).getMajorid() != null && questions.get(j).getMajorid().equals(majorIds.get(i))) {
                    res.add(questions.get(j));
                }
            }
        }
        List<Question> ans = new LinkedList<>();
        for (Question re : res) {
            ans.add(re);
        }
        return ans;
    }


    /**
     * 展示助教所有的学生
     *
     * @param session
     * @return 助教所有学生
     */
    @RequestMapping("/allquestion/student")
    public List<Student> AllStudent(HttpSession session) {
        List<Student> students = studentService.queryAll();
        int assId = (int) session.getAttribute("userId");
        List<Student> res = new LinkedList<>();
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getAssid() == assId) {
                res.add(students.get(i));
            }
        }
        return res;
    }

    /**
     * 点击自动出题后，显示题目
     *
     * @param stuId
     * @param questionId
     * @return
     */
    @RequestMapping("/autCor")
    public List<Subjective> show(Integer stuId, Integer questionId) {
        Integer paperid = 101;//questionService.queryById(questionId).getPaperid();
        Paper paper = paperService.queryById(paperid);
        String question = paper.getPaperquestion();
        String[] split = question.split(",");
        List<Subjective> res = new LinkedList<>();
        for (String s : split) {
            int temp = Integer.parseInt(s);
            if (temp > 3000 && temp < 4000) {
                res.add(subjectiveService.queryById(temp));
            }
        }
        return res;
    }

    /**
     * 批改试卷,自动改卷，前端可展示客观题已经批改完全，请批改主观题
     *
     * @param stuId 传入学生id ,试题id,计入成绩表;
     * @return -1为失败，其他为总分
     */
    @RequestMapping("/autCor/cal")
    public int correct(Integer stuId, Integer questionId, int[] subNum) {
        Integer majorid = studentService.queryById(stuId).getMajorid();
        Question question = questionService.queryById(questionId);
        String questionName = question.getQuestionname();
        Paper paper = paperService.queryById(question.getPaperid());
        String paperQuestion = paper.getPaperquestion();
        String[] split = paperQuestion.split(",");
        if (!majorid.equals(question.getMajorid())) {
            return -1;
        }
        int res = 0;
        for (int i : subNum) {
            if (i > 0 && i < 15) {
                res += i;
            }
        }
        int index1 = 0;
        int index2 = 0;
        int index3 = 0;
        int index4 = 0;
        List<String> ch = new LinkedList<>();
        List<String> fi = new LinkedList<>();
        List<Integer> to = new LinkedList<>();
        for (String s : split) {
            int id = Integer.parseInt(s);
            if (id < 1000) {
                ch.add(choiceService.queryById(id).getChanswer());
            } else if (id < 2000) {
                fi.add(fillbacksService.queryById(id).getAnswer());
            } else if (id < 3000) {
                to.add(torfService.queryById(id).getAnswer());
            }
        }
        Stuans stuans = stuansService.queryBystuAndQues(stuId, questionId);
        String choice = stuans.getChoiceans();
        String fill = stuans.getFillans();
        String torf = stuans.getTorfans();

        String[] sch = choice.split(",");
        String[] sfh = choice.split(",");
        String[] sth = choice.split(",");

        for (int i = 0; i < sch.length; i++) {
            if (ch.get(i).equals(sch[i])) {
                res += 5;
            }
        }
        for (int i = 0; i < sfh.length; i++) {
            if (fi.get(i).equals(sfh[i])) {
                res += 5;
            }
        }
        for (int i = 0; i < sth.length; i++) {
            if (to.get(i).equals(sth[i])) {
                res += 5;
            }
        }
        Grade grade = gradeService.queryByNameAndStu(questionName, stuId);
        if (grade == null) {
            Grade grade1 = new Grade();
            grade1.setGrade(res);
            grade1.setGradename(questionName);
            grade1.setStuid(stuId);
            gradeService.insert(grade1);
        } else {
            grade.setGrade(res);
            gradeService.update(grade);
        }
        return res;
    }

}
